All files / web/src/app/api/vision-training/manifests/[id] route.ts

0% Statements 0/95
0% Branches 0/1
0% Functions 0/1
0% Lines 0/95

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96                                                                                                                                                                                               
import { promises as fs } from 'fs'
import path from 'path'
import { withAuth } from '@/lib/auth/withAuth'

// Force dynamic rendering
export const dynamic = 'force-dynamic'

// Manifest storage directory
const MANIFESTS_DIR = path.join(process.cwd(), 'data/vision-training/manifests')

/**
 * GET /api/vision-training/manifests/[id]
 *
 * Get a manifest by ID.
 */
export const GET = withAuth(
  async (_request, { params }) => {
    const { id } = (await params) as { id: string }

    if (!id) {
      return new Response(JSON.stringify({ error: 'Manifest ID is required' }), {
        status: 400,
        headers: { 'Content-Type': 'application/json' },
      })
    }

    const manifestPath = path.join(MANIFESTS_DIR, `${id}.json`)

    try {
      const content = await fs.readFile(manifestPath, 'utf-8')
      const manifest = JSON.parse(content)

      return new Response(JSON.stringify(manifest), {
        headers: { 'Content-Type': 'application/json' },
      })
    } catch (error) {
      if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
        return new Response(JSON.stringify({ error: 'Manifest not found' }), {
          status: 404,
          headers: { 'Content-Type': 'application/json' },
        })
      }

      console.error(`[Manifests API] Error reading manifest ${id}:`, error)
      return new Response(
        JSON.stringify({ error: 'Failed to read manifest', details: String(error) }),
        { status: 500, headers: { 'Content-Type': 'application/json' } }
      )
    }
  },
  { role: 'admin' }
)

/**
 * DELETE /api/vision-training/manifests/[id]
 *
 * Delete a manifest by ID.
 */
export const DELETE = withAuth(
  async (_request, { params }) => {
    const { id } = (await params) as { id: string }

    if (!id) {
      return new Response(JSON.stringify({ error: 'Manifest ID is required' }), {
        status: 400,
        headers: { 'Content-Type': 'application/json' },
      })
    }

    const manifestPath = path.join(MANIFESTS_DIR, `${id}.json`)

    try {
      await fs.unlink(manifestPath)

      return new Response(JSON.stringify({ success: true, message: 'Manifest deleted' }), {
        headers: { 'Content-Type': 'application/json' },
      })
    } catch (error) {
      if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
        // Already deleted - return success
        return new Response(
          JSON.stringify({ success: true, message: 'Manifest already deleted or does not exist' }),
          { headers: { 'Content-Type': 'application/json' } }
        )
      }

      console.error(`[Manifests API] Error deleting manifest ${id}:`, error)
      return new Response(
        JSON.stringify({ error: 'Failed to delete manifest', details: String(error) }),
        { status: 500, headers: { 'Content-Type': 'application/json' } }
      )
    }
  },
  { role: 'admin' }
)